'''
Created on 2017年5月24日

@author: lingxiang
定时验证代理IP池的IP
'''

import threading
from db import DataBase as db
from verify.ProxyValidation import validUsefulProxy

     
def __init__( *args, **kwargs):
    
    pass


def verifyByTime(proxylist):

    '''验证IP池中(proxylist)的IP  访问百度网站   '''
    for i in proxylist:  
        if validUsefulProxy(i["url"]):
            name_list=["score","status","isOk"]
            val_list=["100","1","1"]
            db.update_proxy(i["url"], name_list, val_list)
        else:
            i["status"]=1
            name_list=["status"]
            val_list=["1"]
            db.update_proxy(i["url"], name_list, val_list)
   
    
'''定时做IP验证  每一分钟执行一次'''    
def runVer():
    proxys_all=db.select_proxy_all(size=500)    #获取500条未检验的代理IP
    '''将列表IP分成三个列表  用三个线程分别执行验证'''
    threadCount = 6
    handleCount=len(proxys_all)//threadCount
    for i in range(0,threadCount):
        handleStartIndex = i*handleCount
        handleEndIndex = (i+1)*handleCount
        if handleEndIndex > len(proxys_all) :
            handleEndIndex = len(proxys_all)
        handleData = proxys_all[handleStartIndex:handleEndIndex]
        t = threading.Thread(target=verifyByTime,args=(handleData,))
        t.start()

        

        



